package com.community.web.master.controller;


import com.community.common.result.Result;
import com.community.model.entity.Role;
import com.community.web.master.domain.dto.AddRoleMenuDto;
import com.community.web.master.domain.dto.PageRoleDto;
import com.community.web.master.domain.dto.RoleDto;
import com.community.web.master.domain.vo.MenuListWithSelectedVo;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/master/role")
@Api(tags = "角色接口")
@Slf4j
public class RoleController {
    @Autowired
    private IRoleService iRoleService;

    /**
     * 分页查询角色
     * @param pageRoleDto
     * @return
     */
    @GetMapping("/pageQuery")
    @ApiOperation("分页查询角色")
    public Result<PageResult> pageQuery(PageRoleDto pageRoleDto){
        log.info("分页查询角色"+pageRoleDto);
        PageResult pageResult = iRoleService.pageQuery(pageRoleDto);
        return Result.success(pageResult);
    }

    @GetMapping
    @ApiOperation("查询所有角色")
    public Result<List<Role>> List(){
        log.info("查询all角色");
        return Result.success(iRoleService.list());
    }

    /**
     * 修改角色信息
     * @param roleDto
     * @return
     */
    @PutMapping
    @ApiOperation("更新角色信息")
    public Result update(@RequestBody RoleDto roleDto){
        log.info("更新角色"+roleDto.getId()+" "+roleDto.getRoleDesc()+" "+roleDto.getRoleState());
        iRoleService.updateUser(roleDto);
        return Result.success();
    }

    /**
     * 新增角色
     * @param roleDto
     * @return
     */
    @PostMapping("/addRole")
    @ApiOperation("新增角色")
    public Result addRole(@RequestBody RoleDto roleDto){
        log.info("新增角色"+roleDto);
        iRoleService.addUser(roleDto);
        return Result.success();
    }

    /**
     * 获得角色的菜单列表
     * @param id
     * @return
     */
    @GetMapping("/selected")
    @ApiOperation("获取角色菜单列表")
    public Result<MenuListWithSelectedVo> getMenuMenuListWithSelected(Integer id){
        log.info("获取角色菜单列表"+id);
        MenuListWithSelectedVo result = iRoleService.getMenuMenuListWithSelected(id);
        return Result.success(result);
    }

    @PutMapping("/addMenu")
    @ApiOperation("赋予角色菜单权限")
    public Result addMenu(@RequestBody AddRoleMenuDto addRoleMenuDto){
        log.info("赋予角色菜单权限"+addRoleMenuDto);
        iRoleService.addMenu(addRoleMenuDto);
        return Result.success();
    }

}
